-- SkyObject
-- Created by chengb Jan/23/2016
-- 天空副本通用建筑格子

-- 初始化
function initSkyObject(self)
    self.item:removeAllChildren();

    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end
            
            -- 如果配置了监狱图标，需要击杀所有怪物后方能拾取
            if self.gridData.need_prison == 1  and
              not DungeonM.isAllMonsterDead() then 
                alert(getLocStr("pick_up_instance_target_fail")); 
                return; 
            end

            local function okFunc()
                local pos = self.gridData:getPos();
                DungeonActionM.go("do_building_bonus", pos);
            end
            self:openBuildingView(okFunc);
        end
    end

    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);
    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);
    
    -- 发光
    if self.gridData.need_shining == 1 then
        shiningEffect(self.icon, 70, 100);
    end

    if self.gridData.need_prison == 1 then
        -- 初始时需要监狱图标的遮罩
        local prisonIcon = getLevelItemIconPath("prison");
        local prisonImg  = ccui.ImageView:create(prisonIcon);
        prisonImg:setName("prison_icon");

        self.item:addChild(prisonImg);
    end
end

